<?php

namespace app\controller\api;

use app\BaseController;
use think\facade\Db;

class AdminController extends BaseController
{

    public function login()
    {
        $username = input("username");
        $password = input("password");

        $where['username'] = $username;
        $where['password'] = $password;
        $userinfo = Db::name("admin")->where($where)->find();
        if (!$userinfo) {
            return retJson([], 201, '账号或密码错误!');
        }
        $token_test = app('jwt')->token($userinfo['id']);

        return retJson(['token' => $token_test]);
    }

    public function userinfo()
    {
        $result = app('auth')->user();

        $data = [
            "username" => "",
            "roles" => [
                "Admin"
            ],
            "permissions" => [
                "read:system",
                "write:system",
                "delete:system"
            ],
            "avatar" => "./static/svg/avatar.svg"
        ];
        $admin_info = Db::name("admin")->where("id", $result['id'])->find();
        $data['username'] = $admin_info['username'];
        return retJson($data);
    }

}
